package com.autonavi.yunda.yunji.service;

import com.autonavi.yunda.yunji.common.exception.AmapException;
import com.autonavi.yunda.yunji.common.exception.AmapExceptionCode;
import com.autonavi.yunda.yunji.core.service.IDynamicConfigService;
import com.autonavi.yunda.yunji.core.vo.localization.DynamicExportVO;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author cizhong.wcs
 * @date 2023/8/31 17:09
 */
@Component
public class DynamicConfigService implements IDynamicConfigService {

    @Override
    public String fetchValue(String projectId, String key) {
        DynamicExportVO dynamic = FileLoaderService.dynamicConfigVOS.stream()
                .filter(vo -> vo.getKeyName().equals(key))
                .findFirst().orElse(null);
        return Optional.ofNullable(dynamic)
                .orElseThrow(() -> new AmapException(AmapExceptionCode.COMMON_SERVER_WARN, "本地化离线部署缺少动态变量:" + key))
                .getValue();
    }
}
